package com.team.childapp.http

import android.content.Context
import android.text.TextUtils
import com.lzy.okgo.model.Response
import com.team.childapp.R
import com.team.childapp.base.App
import com.team.childapp.base.listener.ICallBackResultListener
import com.team.childapp.bean.BaseResponse
import com.team.childapp.bean.constants.ConstantsBean
import com.team.childapp.config.Api
import com.team.childapp.config.BaseConfig
import com.team.childapp.config.RequestID
import com.team.childapp.http.base.AppBaseLogical
import com.team.childapp.http.base.BaseReqBean
import com.team.childapp.http.base.LoadingBaseObserver
import com.team.childapp.util.JsonSerializer
import com.team.childapp.util.LogUtils
import com.team.childapp.util.ToastUtil

/**
 * Create on: 2025/1/4
 * by Mr.Zhu
 * Description: TODO
 */
class ConstantsLogical : AppBaseLogical() {

    companion object {
        private val instance: ConstantsLogical by lazy { ConstantsLogical() }

        fun getInstanceLogin(): ConstantsLogical {
            return instance
        }
    }

    fun requestConstants(context: Context?, resultListener: ICallBackResultListener?) {
        val url = Api.Constants.URL_CONSTANTS
        val reqBean = BaseReqBean()
        requestStringForLogin(
            url,
            reqBean.reqMap,
            CommonObserver(context, RequestID.Constants.CONSTANTS, resultListener)
        )
    }
    fun requestAddContacts(context: Context?,name:String,telephone:String,avatar:Int,resultListener: ICallBackResultListener?) {
        val url = Api.Constants.URL_CONSTANTS_ADD
        val reqBean = BaseReqBean()
        reqBean.map["name"] = name
        reqBean.map["avatar"] = avatar
        reqBean.map["deviceCode"] = 11111
        reqBean.map["telephone"] = telephone
        reqBean.map["userId"] = App.application().getShareData().getString(BaseConfig.USER_ID)
        requestStringForPostNew(
            url,
            reqBean.reqMap,
            CommonObserver(context, RequestID.Constants.CONSTANTS_ADD, resultListener)
        )
    }

    fun requestUpdateContacts(context: Context?,name:String,telephone:String,avatar:Int,id:String,resultListener: ICallBackResultListener?) {
        val url = Api.Constants.URL_CONSTANTS_UPDATE
        val reqBean = BaseReqBean()
        reqBean.map["name"] = name
        reqBean.map["avatar"] = avatar
        reqBean.map["deviceCode"] = 11111
        reqBean.map["telephone"] = telephone
        reqBean.map["id"] = id
        reqBean.map["userId"] = App.application().getShareData().getString(BaseConfig.USER_ID)
        requestStringForPostNew(
            url,
            reqBean.reqMap,
            CommonObserver(context, RequestID.Constants.CONSTANTS_UPDATE, resultListener)
        )
    }


    private class CommonObserver(activity: Context?,
        private val requestID: Int,
        private val resultListener: ICallBackResultListener?
    ) :LoadingBaseObserver() {
        init {
            setShowLoading(activity)
        }

        override fun onNext(stringResponse: Response<String>) {
            super.onNext(stringResponse)
            if (stringResponse.code() != 200 || TextUtils.isEmpty(stringResponse.body())) {
                ToastUtil.showLong(R.string.data_fail)
                return
            }
            val bodyString = stringResponse.body()
            try {
                when (requestID) {
                    RequestID.Constants.CONSTANTS -> {
                        LogUtils.showLog("账号登录 ：$bodyString")
                        val bean: ConstantsBean = JsonSerializer.deSerialize(bodyString,ConstantsBean::class.java)
                        if (bean.isDataSuccess()) {
                            resultListener?.onCallBack(bean)
                        } else {
                            ToastUtil.showLong(bean.msg)
                            resultListener?.onCallBack(null)
                        }
                    }
                    RequestID.Constants.CONSTANTS_ADD -> {
                        resultListener?.onCallBack(true)
                    }RequestID.Constants.CONSTANTS_UPDATE -> {
                    resultListener?.onCallBack(true)
                }
                }
            } catch (e: Exception) {
                ToastUtil.showLong(R.string.data_exception)
                resultListener?.onCallBack(0)
            }

        }

        override fun onError(e: Throwable) {
            super.onError(e)
            resultListener?.onCallBack(null)

        }
    }
}